package gui.empleado;

import javax.swing.JPanel;

import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.SystemColor;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableRowSorter;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.RowFilter;

import java.lang.String;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;

import javax.swing.JButton;

import modelo.ModeloEmpleado;
import javax.swing.JCheckBox;

public class PanelEmpleado extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelEmpleado = null;
	private JPanel panelBuscar = null;
	private JLabel lblBuscar = null;
	private JComboBox cmbBuscar = null;
	private JTextField txtBuscar = null;
	private JButton btnAgregar = null;
	private JButton btnModificar = null;
	private JScrollPane jspEmpleado = null;
	private JTable tblEmpleado = null;
	private JButton btnCambiarEstatus = null;
	private ModeloEmpleado modeloEmpleado = null;
	private JCheckBox checkBoxActivo = null;
	private JCheckBox checkBoxInactivo = null;
	private JCheckBox checkBoxTodosEmpleados = null;
	private int indiceComboBuscar = 0;
	private JButton btnImprimir = null;

	/**
	 * This is the default constructor
	 */
	public PanelEmpleado() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {

		this.modeloEmpleado = ModeloEmpleado.getInstancia();
		this.setSize(884, 378);
		this.setLayout(null);
		this.add(getPanelEmpleado(), null);
		this.add(getPanelBuscar(), null);
		this.add(getBtnAgregar(), null);
		this.add(getBtnModificar(), null);
		this.add(getBtnCambiarEstatus(), null);
		this.add(getCheckBoxActivo(), null);
		this.add(getCheckBoxInactivo(), null);
		this.add(getCheckBoxTodosEmpleados(), null);
		this.add(getBtnImprimir(), null);
	}

	/**
	 * This method initializes panelEmpleado
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelEmpleado() {
		if (panelEmpleado == null) {
			panelEmpleado = new JPanel();
			panelEmpleado.setLayout(null);
			panelEmpleado.setBounds(new Rectangle(6, 90, 871, 227));
			panelEmpleado.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(SystemColor.controlText, 1), "Lista de Empleados", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 14), SystemColor.controlText));
			panelEmpleado.add(getJspEmpleado(), null);

		}
		return panelEmpleado;
	}

	/**
	 * This method initializes jspEmpleado
	 * 
	 * @return javax.swing.JScrollPane
	 */
	/**
	 * This method initializes panelBuscar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelBuscar() {
		if (panelBuscar == null) {
			lblBuscar = new JLabel();
			lblBuscar.setBounds(new Rectangle(10, 21, 55, 23));
			lblBuscar.setText("Buscar:");
			panelBuscar = new JPanel();
			panelBuscar.setLayout(null);
			panelBuscar.setBounds(new Rectangle(255, 18, 445, 58));
			panelBuscar.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(Color.black, 1), "Busqueda",
					TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("SansSerif",
							Font.PLAIN, 12), Color.black));
			panelBuscar.add(lblBuscar, null);
			panelBuscar.add(getCmbBuscar(), null);
			panelBuscar.add(getTxtBuscar(), null);
		}
		return panelBuscar;
	}

	/**
	 * This method initializes cmbBuscar
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCmbBuscar() {
		if (cmbBuscar == null) {
			cmbBuscar = new JComboBox();
			cmbBuscar.setBounds(new Rectangle(67, 21, 151, 23));
			cmbBuscar.setModel(new DefaultComboBoxModel(new String[] {
					"ID Empleado", "Nombre", "Apellido", "Cedula" }));
			cmbBuscar.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					indiceComboBuscar = cmbBuscar.getSelectedIndex();
				}
			});
		}
		return cmbBuscar;
	}

	/**
	 * This method initializes txtBuscar
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtBuscar() {
		if (txtBuscar == null) {
			txtBuscar = new JTextField();
			txtBuscar.setBounds(new Rectangle(221, 21, 210, 23));
			txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_BACK_SLASH) {
						getTxtBuscar().setText("");
					}
					TableRowSorter<ModeloEmpleado> sorter = new TableRowSorter<ModeloEmpleado>(
							modeloEmpleado);
					tblEmpleado.setRowSorter(sorter);
					sorter.setRowFilter(RowFilter.regexFilter("(?i)"
							.concat(getTxtBuscar().getText()),
							indiceComboBuscar));

				}
			});

		}
		return txtBuscar;
	}

	/**
	 * This method initializes btnAgregar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAgregar() {
		if (btnAgregar == null) {
			btnAgregar = new JButton();
			btnAgregar.setBounds(new Rectangle(259, 332, 108, 33));
			btnAgregar.setText("Agregar");
			btnAgregar.setIcon(new ImageIcon("./lib/images/add.png"));
			btnAgregar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DialogoAgregarEmpleado dialogoAgregarEmpleado = DialogoAgregarEmpleado
							.getInstancia();
					dialogoAgregarEmpleado.setVisible(true);
				}
			});
		}
		return btnAgregar;
	}

	/**
	 * This method initializes btnModificar s
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnModificar() {
		if (btnModificar == null) {
			btnModificar = new JButton();
			btnModificar.setBounds(new Rectangle(378, 332, 108, 33));
			btnModificar.setText("Modificar");
			btnModificar.setIcon(new ImageIcon("./lib/images/modify.png"));
			btnModificar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (tblEmpleado.isRowSelected(tblEmpleado.getSelectedRow()) == false) {
						JOptionPane.showMessageDialog(null,
								"Seleccione el Empleado a Modificar");
					} else {

						int id = Integer.parseInt(tblEmpleado.getValueAt(
								tblEmpleado.getSelectedRow(), 0).toString());
						PanelModificarEmpleado panelModificarEmpleado = PanelModificarEmpleado
								.getInstancia();
						panelModificarEmpleado.setIdModificar(id);
						panelModificarEmpleado.llenarCampos();
						DialogoModificarEmpleado.getInstancia()
								.setVisible(true);
					}

				}
			});
		}
		return btnModificar;
	}

	/**
	 * This method initializes jspEmpleado
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJspEmpleado() {
		if (jspEmpleado == null) {
			jspEmpleado = new JScrollPane();
			jspEmpleado.setBounds(new Rectangle(12, 25, 850, 185));
			jspEmpleado.setViewportView(getTblEmpleado());
		}
		return jspEmpleado;
	}

	/**
	 * This method initializes tblEmpleado
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTblEmpleado() {
		if (tblEmpleado == null) {
			tblEmpleado = new JTable(modeloEmpleado);
		}
		return tblEmpleado;
	}

	/**
	 * This method initializes btnCambiarEstatus
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnCambiarEstatus() {
		if (btnCambiarEstatus == null) {
			btnCambiarEstatus = new JButton();
			btnCambiarEstatus.setBounds(new Rectangle(497, 332, 150, 33));
			btnCambiarEstatus.setText("Cambiar Estatus");
			btnCambiarEstatus.setIcon(new ImageIcon("./lib/images/change.png"));
			btnCambiarEstatus
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (tblEmpleado.isRowSelected(tblEmpleado
									.getSelectedRow()) == false) {
								JOptionPane
										.showMessageDialog(
												null,
												"Seleccione el Empleado al cual le desea Cambiar el Estatus"
												);
							} else {
								int opcion = JOptionPane
										.showConfirmDialog(
												null,
												"<html>Esta Seguro que desea cambiarle<br> el Estatus a este Empleado?</html>",
												"Confirmando",
												JOptionPane.YES_NO_OPTION);
								if (opcion == JOptionPane.YES_OPTION) {
									int id = Integer.parseInt(tblEmpleado
											.getValueAt(
													tblEmpleado
															.getSelectedRow(),
													0).toString());
									modeloEmpleado.cambiarEstatus(id);
									JOptionPane
											.showMessageDialog(
													null,
													"El Estatus del empleado fue Cambiado satisfactoriamente",
													"Informacion",
													JOptionPane.INFORMATION_MESSAGE);
								}
							}

						}
					});
		}
		return btnCambiarEstatus;
	}

	/**
	 * This method initializes checkBoxActivo
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxActivo() {
		if (checkBoxActivo == null) {
			checkBoxActivo = new JCheckBox();
			checkBoxActivo.setBounds(new Rectangle(709, 20, 156, 23));
			checkBoxActivo.setName("ACTIVO");
			checkBoxActivo.setText("Empleados Activos");
			checkBoxActivo
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (checkBoxActivo.isSelected() == true) {
								checkBoxInactivo.setSelected(false);
								checkBoxTodosEmpleados.setSelected(false);
								modeloEmpleado
										.actualizarTableParametros(checkBoxActivo
												.getName());
							}
						}
					});

		}
		return checkBoxActivo;
	}

	/**
	 * This method initializes checkBoxInactivo
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxInactivo() {
		if (checkBoxInactivo == null) {
			checkBoxInactivo = new JCheckBox();
			checkBoxInactivo.setBounds(new Rectangle(709, 39, 156, 23));
			checkBoxInactivo.setName("INACTIVO");
			checkBoxInactivo.setText("Empleados Inactivos");
			checkBoxInactivo
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (checkBoxInactivo.isSelected() == true) {
								checkBoxActivo.setSelected(false);
								checkBoxTodosEmpleados.setSelected(false);
								modeloEmpleado
										.actualizarTableParametros(checkBoxInactivo
												.getName());
							}
						}
					});
		}
		return checkBoxInactivo;
	}

	/**
	 * This method initializes checkBoxTodosEmpleados
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxTodosEmpleados() {
		if (checkBoxTodosEmpleados == null) {
			checkBoxTodosEmpleados = new JCheckBox();
			checkBoxTodosEmpleados.setBounds(new Rectangle(709, 58, 156, 23));
			checkBoxTodosEmpleados.setText("Todos los Empleados");
			checkBoxTodosEmpleados.setName("TODOS");
			checkBoxTodosEmpleados
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (checkBoxTodosEmpleados.isSelected() == true) {
								checkBoxInactivo.setSelected(false);
								checkBoxActivo.setSelected(false);
								modeloEmpleado
										.actualizarTableParametros(checkBoxTodosEmpleados
												.getName());
							}
						}
					});

		}
		return checkBoxTodosEmpleados;
	}

	/**
	 * This method initializes btnImprimir	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnImprimir() {
		if (btnImprimir == null) {
			btnImprimir = new JButton();
			btnImprimir.setBounds(new Rectangle(23, 54, 201, 28));
			btnImprimir.setText("Imprimir Lista de Empleados");
			btnImprimir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						tblEmpleado.print();
					} catch (PrinterException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return btnImprimir;
	}

} // @jve:decl-index=0:visual-constraint="162,47"
